package com.zhuss.controller.food;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zhuss.bean.bo.food.product.CreateProductBo;
import com.zhuss.bean.bo.food.product.DownProductBo;
import com.zhuss.bean.bo.food.product.UpdateProductBo;
import com.zhuss.bean.bo.food.product.UpperProductBo;
import com.zhuss.bean.bo.food.product.takeout.CreateTakeoutBo;
import com.zhuss.bean.bo.food.product.takeout.UpdateTakeoutBo;
import com.zhuss.common.BaseController;
import com.zhuss.common.OperateService;
import com.zhuss.exception.WpsException;
import com.zhuss.food.product.ProductOperateServiceExt;
import com.zhuss.util.DateUtil;


/**
 * 外卖打包操作 controller
 * @Author zhushanshuo
   @Date 2020/03/01 16:01
 *
 */
  
 
@RestController
@RequestMapping("/takeout")
public class TakeoutOperateController extends BaseController {
	
	@Autowired
	@Qualifier("productOperateService")
	private OperateService<Void, Object> productOperateService;
	
	@Autowired
	@Qualifier("productOperateServiceExt")
	private ProductOperateServiceExt productOperateServiceExt;
	
	/**
	 * 创建
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/create")
	@Transactional(rollbackFor = Exception.class)
    public void create(@RequestBody CreateTakeoutBo bo) throws WpsException {
		CreateProductBo param = new CreateProductBo();
		BeanUtils.copyProperties(bo, param);
		param.setDate(DateUtil.toDateOnly(bo.getDate()));
		param.setStartTime(param.getDate());
		param.setEndTime(DateUtil.toDateOnlyEnd(bo.getDate()));
		param.setFoodId(bo.getFoodId());
		param.setPrice(bo.getPrice());
		param.setTotal(bo.getTotal());
		param.setType(bo.getType());
		param.setUsed(0l);
		productOperateService.create(param);
    }
	
	/**
	 * 批量创建
	 * @param bo
	 * @throws WpsException
	 *//*
	@ResponseBody
    @PostMapping("/batchCreate")
    @Transactional(rollbackFor = Exception.class)
    public void batchCreate(@RequestBody CreateTakeoutBo bo) throws WpsException {
		List<Long> foodIdList = bo.getFoodIdList();
		for (Long foodId : foodIdList) {
			CreateProductBo param = new CreateProductBo();
			BeanUtils.copyProperties(bo, param);
			param.setDate(DateUtil.toDateOnly(bo.getDate()));
			param.setStartTime(param.getDate());
			param.setEndTime(DateUtil.toDateOnlyEnd(bo.getDate()));
			param.setFoodId(foodId);
			param.setPrice(bo.getPrice());
			param.setTotal(bo.getTotal());
			param.setType(bo.getType());
			param.setUsed(0l);
			productOperateService.create(param);
		}
		
    }*/
	
	
	/**
	 * 修改
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/update")
    public void update(@RequestBody UpdateTakeoutBo bo) throws WpsException {
		UpdateProductBo param = new UpdateProductBo();
		BeanUtils.copyProperties(bo, param);
		param.setProductId(bo.getProductId());
		param.setPrice(bo.getPrice());
		param.setTotal(bo.getTotal());
		param.setType(bo.getType());
		productOperateService.update(param);
    }
	
	/**
	 * 删除
	 * @param bo
	 * @throws WpsException
	 *//*
	@ResponseBody
    @PostMapping("/delete")
    public void delete(@RequestBody DeleteProductBo bo) throws WpsException {
		
		productOperateService.delete(bo);
    }*/
	
	
	/**
	 * 上架
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/upper")
    public void upper(@RequestBody UpperProductBo bo) throws WpsException {
		
		productOperateServiceExt.upper(bo);
    }
	
	
	/**
	 * 下架
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/down")
    public void down(@RequestBody DownProductBo bo) throws WpsException {
		
		productOperateServiceExt.down(bo);
    }
	
}
